package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.QueryPageParam;
import com.example.common.Result;
import com.example.pojo.Title;
import com.example.pojo.Topic;
import com.example.pojo.User;
import com.example.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 成绩表 前端控制器
 * </p>
 *
 * @author zzw
 * @since 2023-10-29
 */
@RestController
@RequestMapping("/topic")
public class TopicController {
    @Autowired
    private TopicService topicService;
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {

        HashMap param = query.getParam();
        String number = (String)param.get("number");
        String name = (String)param.get("name");
        String instructor = (String)param.get("instructor");
        String account = (String)param.get("account");
        String grade = (String)param.get("grade");
        Page<Topic> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        LambdaQueryWrapper<Topic> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(number) && !"null".equals(number)) {
            lambdaQueryWrapper.like(Topic::getNumber, number);
        }
        if(StringUtils.isNotBlank(name) && !"null".equals(name)) {
            lambdaQueryWrapper.like(Topic::getName, name);
        }
        if(StringUtils.isNotBlank(instructor) && !"null".equals(instructor)) {
            lambdaQueryWrapper.like(Topic::getInstructor, instructor);
        }
        if(StringUtils.isNotBlank(account) && !"null".equals(account)) {
            lambdaQueryWrapper.like(Topic::getAccount, account);
        }
        if(StringUtils.isNotBlank(grade) && !"null".equals(grade)) {
            lambdaQueryWrapper.eq(Topic::getGrade, grade);
        }
        IPage result = topicService.page(page, lambdaQueryWrapper);
        return Result.success(result.getRecords(), result.getTotal());
    }

    @GetMapping("/findByAccount")
    public Result findByNo(@RequestParam String account) {

        List<Topic> list = topicService.lambdaQuery().eq(Topic::getAccount, account).list();

        return list.size() > 0 ? Result.success(list) : Result.fall();
    }


    @GetMapping("/delete")
    public Result delete(@RequestParam String id) {
        return topicService.removeById(id) ? Result.success() : Result.fall();
    }

    //增加
    @PostMapping("/save")
    public Result save(@RequestBody Topic topic) {
        return topicService.save(topic) ? Result.success() : Result.fall();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Topic topic) {
        return topicService.updateById(topic) ? Result.success() : Result.fall();
    }


}
